package com.battcn.platform.service.sys.impl;

import com.battcn.framework.mybatis.service.impl.BaseServiceImpl;
import com.battcn.platform.mapper.sys.RoleMapper;
import com.battcn.platform.mapper.sys.RoleOperateMapper;
import com.battcn.platform.pojo.po.sys.Role;
import com.battcn.platform.service.sys.RoleService;
import com.battcn.platform.util.SessionUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.orderbyhelper.OrderByHelper;

import java.util.List;

/**
 * @author Levin
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    private final RoleMapper roleMapper;
    private final RoleOperateMapper roleOperateMapper;

    @Autowired
    public RoleServiceImpl(RoleMapper roleMapper, RoleOperateMapper roleOperateMapper) {
        this.roleMapper = roleMapper;
        this.roleOperateMapper = roleOperateMapper;
    }

    @Override
    public List<Role> listRole() {
        OrderByHelper.orderBy("create_date ASC");
        List<Role> roles = roleMapper.selectAll();
        //如果当前登陆者不是超级管理员，列表不显示超级管理员角色
        if(!SessionUtil.getSession().getId().equals("1")){
            for(Role r : roles){
                if(r.getId().equals("1")){
                    roles.remove(r);
                    break;
                }
            }
        }
        return roles;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRoleAndOperate(String[] ids) {
        Lists.newArrayList(ids).forEach(id -> {
            //超级管理员角色不能删除
            if(!id.equals("1")) {
                super.deleteById(id);
                this.roleOperateMapper.deleteRoleOperateByRoleId(id);
            }
        });
    }

}
